package siga.visao_busca;

import java.sql.SQLException;
import siga.banco.conexao;
import siga.visao.index;
import siga.visao.verErros;

public class buscaSalas_Laboratorios extends javax.swing.JDialog {
    public String filtro="";
    public buscaSalas_Laboratorios(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtfDescricao = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtDisciplina = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Buscando Salas e Laboratórios");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("Código:");
        jLabel1.setName("jLabel1"); // NOI18N

        jtfCodigo.setName("jtfCodigo"); // NOI18N
        jtfCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfCodigoKeyPressed(evt);
            }
        });

        jLabel2.setText("Descrição:");
        jLabel2.setName("jLabel2"); // NOI18N

        jtfDescricao.setName("jtfDescricao"); // NOI18N
        jtfDescricao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDescricaoKeyPressed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jButton1.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtDisciplina.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Tipo", "Descrição"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtDisciplina.setName("jtDisciplina"); // NOI18N
        jtDisciplina.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtDisciplinaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtDisciplina);
        jtDisciplina.getColumnModel().getColumn(0).setPreferredWidth(70);
        jtDisciplina.getColumnModel().getColumn(0).setMaxWidth(70);
        jtDisciplina.getColumnModel().getColumn(1).setMinWidth(150);
        jtDisciplina.getColumnModel().getColumn(1).setPreferredWidth(150);
        jtDisciplina.getColumnModel().getColumn(1).setMaxWidth(150);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jtfDescricao, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18))))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 704, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        busca();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jtDisciplinaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtDisciplinaMouseClicked
        if(evt.getClickCount()==2){
            index.LRsalaselaboratorios.add(0,jtDisciplina.getValueAt(jtDisciplina.getSelectedRow(), 0).toString());
            index.LRsalaselaboratorios.add(1,jtDisciplina.getValueAt(jtDisciplina.getSelectedRow(), 2).toString());
            dispose();
        }
    }//GEN-LAST:event_jtDisciplinaMouseClicked

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened

    }//GEN-LAST:event_formWindowOpened

    private void busca(){
        try {
            String codigo;
            String descricao;
            if (jtfCodigo.getText().equals("")) {codigo = "";} else {codigo = " and codigo=" + jtfCodigo.getText();}
            if (jtfDescricao.getText().equals("")) {descricao = "";} else {descricao = " and descricao like \"" + jtfDescricao.getText() + "%\"";}
            conexao.Query("select * from salas_laboratorios where cod_emp=" + index.cod_emp + codigo + " " + descricao + " and tipo=\""+filtro+"\" order by tipo, descricao");
            javax.swing.table.DefaultTableModel jtd = (javax.swing.table.DefaultTableModel) jtDisciplina.getModel();
            for (int x = jtDisciplina.getRowCount() - 1; x >= 0; --x) {
                jtd.removeRow(x);
            }
            while (conexao.rs.next()) {
                jtd.addRow(new Object[]{conexao.rs.getString("codigo"), conexao.rs.getString("tipo"), conexao.rs.getString("descricao")});
            }
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }

    private void jtfCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfCodigoKeyPressed
        if(evt.getKeyCode()==10){
            busca();
        }
    }//GEN-LAST:event_jtfCodigoKeyPressed

    private void jtfDescricaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDescricaoKeyPressed
        if(evt.getKeyCode()==10){
            busca();
        }
    }//GEN-LAST:event_jtfDescricaoKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtDisciplina;
    private javax.swing.JTextField jtfCodigo;
    private javax.swing.JTextField jtfDescricao;
    // End of variables declaration//GEN-END:variables

}
